package com.mycjj.android.activity;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.mycjj.android.MyCjjApplication;
import com.mycjj.android.ProjectDataManage;
import com.mycjj.android.R;
import com.mycjj.android.adapter.TAdapter;
import com.mycjj.android.bean.TreeElement;
import com.mycjj.android.bean.UserCarGetsBaseBean;
import com.mycjj.android.bean.UserCarGetsBaseBean.UserCarGetsBean;
import com.mycjj.android.http.ProjectHttpRequestImpl;
import com.mycjj.android.http.ProjectHttpRequestInterface;
import com.mycjj.android.util.Preferences;

public class CarListActivity extends BaseActivity{
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_car_list);
		mActManager.pushActivity(YUYUE_KEY, this);
		init();
	}
	
	private ImageView mHeaderLeftImageview;
	private Button mHeaderRightButton;
	private TextView mHeaderTitleTextView;
	private void initHeader(){
		mHeaderLeftImageview = (ImageView) findViewById(R.id.header_left_button);
		mHeaderLeftImageview.setImageResource(R.drawable.back);
		mHeaderRightButton = (Button) findViewById(R.id.header_right_button);
		ViewGroup.LayoutParams params = (ViewGroup.LayoutParams)mHeaderRightButton.getLayoutParams();
		params.height = (int)(100 * MyCjjApplication.getInstance().getDisplayDensity());
		params.width = (int)(100 * MyCjjApplication.getInstance().getDisplayDensity());
		mHeaderRightButton.setBackgroundResource(R.color.transparent);
		mHeaderRightButton.setLayoutParams(params);
		mHeaderRightButton.setText("添加车辆");
		mHeaderRightButton.setTextColor(getResources().getColor(R.color.white));
		mHeaderRightButton.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View v) {
				Intent intent = new Intent(CarListActivity.this,CarInfoActivity.class);
				intent.putExtra("car_state", 2);
				startActivity(intent);
			}
		});
		mHeaderTitleTextView = (TextView) findViewById(R.id.header_title_textview);
		mHeaderTitleTextView.setText("车辆列表");
		mHeaderLeftImageview.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				mActManager.popActivity(YUYUE_KEY, CarListActivity.this);
			}
		});
	}
	private ListView listview;
	private TextView tv_empty;
	private void init(){
		initHeader();
		listview = (ListView) findViewById(R.id.listview);
		tv_empty = (TextView)findViewById(R.id.tv_empty);
		adapter = new CarListAdapter(this);
		listview.setAdapter(adapter);
		listview.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {
				//TODO: need to set Tree elements
				UserCarGetsBean bean = dataList.get(position);
				TreeElement element = dataManager.getElement();
				if(element == null){
					element = new TreeElement();
					element.setBrandId(String.valueOf(bean.getBrand1_id()));
					element.setBrandId3(String.valueOf(bean.getId()));
					dataManager.setElement(element);
				}else{
					element.setBrandId(String.valueOf(bean.getBrand1_id()));
				}
				Intent intent = new Intent(CarListActivity.this,SelectYuyueTypeActivity.class);
				intent.putExtra("carId", String.valueOf(bean.getId()));
				startActivity(intent);
			}
		});
		
	}
	@Override
	protected void onResume() {
		super.onResume();
		getData();
	}
	 List<UserCarGetsBean> dataList;
	private void getData(){
		request = new ProjectHttpRequestImpl(this, true);
		RequestParams params = new RequestParams();
		params.put("client_id",ProjectHttpRequestInterface.client_id_key);
		params.put("token", Preferences.getSharedPreferences(this, "user_token", ""));
		request.requestUserCarGetsWithDoneHandler("", params, new AsyncHttpResponseHandler(){
			@Override
			public void onSuccess(String content) {
				UserCarGetsBaseBean baseBean = ProjectDataManage.getInstance().getUserCarGetsBaseBean();
				if(baseBean != null){
					if(baseBean.getCode() == 200){
						dataList = baseBean.getData();
						if(dataList != null && dataList.size() > 0){
							listview.setVisibility(View.VISIBLE);
							tv_empty.setVisibility(View.GONE);
							adapter.setList(dataList);
						}else{
							listview.setVisibility(View.GONE);
							tv_empty.setVisibility(View.VISIBLE);
						}
					}else{
						Toast.makeText(CarListActivity.this, baseBean.getMsg(), Toast.LENGTH_SHORT).show();
					}
				}else{
					Toast.makeText(CarListActivity.this, "UserCarGetsBaseBean is empty!!", Toast.LENGTH_SHORT).show();
				}
			}
		});
	}
	private CarListAdapter adapter;
	public class CarListAdapter extends TAdapter<UserCarGetsBean>{

		public CarListAdapter(Context mContext) {
			super(mContext);
		}
		
		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder;
			if (convertView == null) {
				convertView = LayoutInflater.from(mContext).inflate(R.layout.item_car_list_view, null);
				holder = new ViewHolder();
				holder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
				holder.pic = (ImageView)convertView.findViewById(R.id.header_pic);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			UserCarGetsBean bean = mList.get(position);
			imageLoader.displayImage(bean.getBrand_logo(), holder.pic);
			holder.tv_name.setText(bean.getCar_name());
			return convertView;
		}
		
		
		class ViewHolder{
			TextView tv_name;
			ImageView pic;
		}
		
		

	}
	
	
	
}
